package me.pplay.playtalk.adapter;


import me.pplay.playtalk.R;
import me.pplay.playtalk.holder.GridMenuHolder;
import me.pplay.playtalk.util.AsyncImageLoader;
import me.pplay.playtalk.util.AsyncImageLoader.ImageCallback;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class GridMenuAdapter extends BaseAdapter {
	private Context mContext;
	private int[] menu_drawableIds; //= {R.drawable.icon_1, R.drawable.icon_2, R.drawable.icon_3, R.drawable.icon_4, R.drawable.icon_5, R.drawable.icon_6};
	private String[] menu_titles;
	private LayoutInflater inflater;
	private boolean bool;
	private String drawableUrl;
	private int drawableId;
	private AsyncImageLoader imageLoader;
	
	public GridMenuAdapter(Context context, int[] menu_drawableIds, String[] menu_titles, boolean bool){
		this.mContext			= context;
		this.menu_drawableIds	= menu_drawableIds;//= context.getResources().getStringArray(R.array.array_multimedia);
		this.menu_titles		= menu_titles;
		this.bool				= bool;
	}
	
	public GridMenuAdapter(Context context,int drawableId, String drawableUrl,String[] menu_titles, boolean bool, AsyncImageLoader imageLoader){
		this.mContext			= context;
		this.menu_titles		= menu_titles;
		this.bool				= bool;
		this.drawableId			= drawableId;
		this.drawableUrl		= drawableUrl;
		this.imageLoader		= imageLoader;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return menu_titles.length;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return menu_titles[position];
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		final GridMenuHolder holder;
		if(convertView == null){
			holder 		= new GridMenuHolder();
			inflater	= LayoutInflater.from(mContext);
			convertView	= inflater.inflate(R.layout.grid_menu_item, null);
			holder.menu_icon	= (ImageView)convertView.findViewById(R.id.item_image);
			holder.menu_title	= (TextView)convertView.findViewById(R.id.item_text);
			convertView.setTag(holder);
		}else{
			holder		= (GridMenuHolder)convertView.getTag();
		}
		if(bool){
			holder.menu_icon.setBackgroundResource(menu_drawableIds[position]);
		}else{
			if(position == 0)
				holder.menu_icon.setBackgroundResource(drawableId);
			else
				setImageIcon(holder.menu_icon, drawableUrl);
		}
		holder.menu_title.setText(menu_titles[position]);
		return convertView;
	}
	
	private void setImageIcon(final ImageView imageView, final String imageUrl){
		if(imageUrl == null){
			imageView.setImageResource(R.drawable.ic_launcher);
			return;
		}
		Bitmap bitmap = imageLoader.loadBitmap(imageUrl, new ImageCallback() {
			
			@Override
			public void imageLoaded(Bitmap imageBitmap, String imageUrl) {
				if(imageBitmap != null)
					imageView.setImageBitmap(imageBitmap);
			}
		});
		if(bitmap != null)
			imageView.setImageBitmap(bitmap);
	}

}
